//
//  ViewController.m
//  Animation
//
//  Created by cocoazxj@163.com on 15/4/21.
//  Copyright (c) 2015年 handcool. All rights reserved.
//

#import "ViewController.h"

#define transform(angle) ((angle)/180.0 * M_PI)

@interface ViewController ()
@property (nonatomic, strong) IBOutlet UIImageView *iv_icon;
@property (nonatomic, strong) CALayer *pLayer;
@property (nonatomic, assign) int pIndex;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    CALayer *lay = [CALayer layer];
    lay.frame = CGRectMake(20, 20, 40, 40);
//    lay.bounds = CGRectMake(100, 100, 40, 40);
    lay.backgroundColor = [UIColor redColor].CGColor;
//    lay.anchorPoint = CGPointZero;
    // 实现代理就可以，在CALayer绘制图形；
    lay.delegate = self;
    // 调用这个方法，显示重新绘制的图形；
    [lay setNeedsDisplay];
    self.pLayer = lay;
    [self.view.layer addSublayer:lay];
}

// CALayer绘制图形的代理方法
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
    CGContextMoveToPoint(ctx, 20, 20);
    CGContextAddLineToPoint(ctx, 30, 50);
    CGContextStrokePath(ctx);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//    [self basicAnimation];
//    [self keyAnimation];
    [self groupAnimation];
}

// 基础动画
- (void)basicAnimation
{
    // transform bounds
    CABasicAnimation *basic = [CABasicAnimation animation];
//    basic.keyPath = @"position";
//    basic.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 200)];
    basic.keyPath = @"transform.scale";
    basic.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.5, 1.5, 0)];
    
    basic.removedOnCompletion = NO;
    basic.fillMode = kCAFillModeForwards;
    [self.pLayer addAnimation:basic forKey:nil];
}

// 关键帧动画
- (void)keyAnimation
{
    CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animation];
//    keyFrame.keyPath = @"position";
    keyFrame.keyPath = @"transform.rotation";
    // 方形轨迹
//    NSValue *v1 = [NSValue valueWithCGPoint:CGPointMake(150, 0)];
//    NSValue *v2 = [NSValue valueWithCGPoint:CGPointMake(150, 300)];
//    NSValue *v3 = [NSValue valueWithCGPoint:CGPointMake(0, 300)];
//    NSValue *v4 = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
//    keyFrame.values = @[v1,v2,v3,v4];
    // 圆形轨迹
//    CGMutablePathRef mp = CGPathCreateMutable();
//    CGPathAddEllipseInRect(mp, NULL, CGRectMake(0, 0, 200, 200));
//    keyFrame.path = mp;
//    CGPathRelease(mp);
//    keyFrame.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    
    // 抖动
    keyFrame.values = @[@(-transform(5)),@(transform(5)), @(-transform(5))];
    keyFrame.duration = 0.5;
    keyFrame.repeatCount = MAXFLOAT;
    keyFrame.delegate = self;
    
    [self.pLayer addAnimation:keyFrame forKey:@"keyFrameAnimation"];
}

// 关键帧动画的代理方法
- (void)animationDidStart:(CAAnimation *)anim
{
    NSLog(@"animationDidStart");
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    NSLog(@"animationDidStop");
}

// 取消动画
- (IBAction)cancelButton:(id)sender
{
    [self.pLayer removeAnimationForKey:@"keyFrameAnimation"];
}

// 转场动画
- (IBAction)down:(id)sender
{
    self.pIndex ++;
    if(self.pIndex == 4)
    {
        self.pIndex = 1;
    }
        [self.iv_icon setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d",self.pIndex]]];
    [self transitionAnimation:self.iv_icon.layer direction:kCATransitionFromRight];
}

- (IBAction)up:(id)sender
{
    
}

- (void)transitionAnimation:(CALayer *)lay direction:(NSString *)direction
{
    CATransition *trans = [CATransition animation];
    trans.type = @"cube";
    trans.subtype = direction;
    trans.duration = 2.0;
    trans.startProgress = 0.8;
    [lay addAnimation:trans forKey:direction];
}

// 组动画
- (void)groupAnimation
{
    CABasicAnimation *b1 = [CABasicAnimation animation];
    b1.keyPath = @"position";
    b1.toValue = [NSValue valueWithCGPoint:CGPointMake(20, 40)];
    
    CABasicAnimation *b2 = [CABasicAnimation animation];
    b2.keyPath = @"transform.rotation";
    b2.toValue = @(M_PI_4);
    
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.animations = @[b1,b2];
    group.duration = 2.0;
    group.removedOnCompletion = NO;
    group.fillMode = kCAFillModeForwards;
    [self.iv_icon.layer addAnimation:group forKey:@"group"];
}

// 关闭隐式动画
- (void)closeConceal
{
    [CATransaction begin];
    [CATransaction setDisableActions:YES];
    self.pLayer.backgroundColor = [UIColor blackColor].CGColor;
    self.pLayer.position = CGPointMake(50, 50);
    [CATransaction commit];
}
@end
